|
|
|
|
|
|
|
Each object in a Visual Basic program can raise a specific group of events, depending on the object's type (text box, form, command button, and so on). However, the key to handling events is to determine exactly which object/event combinations are appropriate to your program and then write code for those object/event combinations. You do so by creating an event procedure in a Code window. For example, if you want to write some code that will be executed whenever a command button named cmdExit is clicked, you write the Click event procedure for cmdExit. You can edit this procedure by opening a Code window on the form that contains cmdExit. |
|
|
|
|
|
|
|
|
Recall from Chapter 2, "Creating Your First Program," that you use the drop-down boxes at the top of the Code window to select an event procedure. The object is selected in the Code window's upper-left drop-down list box (the Object box); the appropriate event procedure is selected in the upper-right drop-down list box (the Procedure box). When that object/event combination occurs at runtime, if code has been written for that combination, Visual Basic executes the code. If no code has been written for the combination, Visual Basic ignores the event. Figure 5.2 shows an example of an event procedure written for a specific control and event. |
|
|
|
|
|
|
|
|
Figure 5.2
Visual Basic responds to an event only if you write code for it. |
|
|
|
|
|
|
|
|
Two basic types of events can occur in your Visual Basic program: user-initiated events and system-initiated events. Most often, you will write code for user-initiated events, which let your users control the flow of the program. That is, your users can take a specific action whenever they want (within the constraints imposed by the program), which gives them almost complete control. |
|
|
|
|
|
|
|
|
|
You can, of course, limit the actions that users can take by hiding or disabling controls when you don't want users to have access to them. This technique is discussed in Chapter 3, "Visual Basic Building Blocks."  |
|
|
|
|
|
|
|
|
|
|
See "Using Properties to Control User Interaction," p. 48 |
|
|
|
|
|
|
|
|
User-initiated events are those that occur because of an action taken by the user. As you might guess, these events include keystrokes and mouse clicks, but other events are also caused by the user, either directly or indirectly. For example, when the user clicks a text box to start editing the information in the box, a Click event is fired for the text box. What you might not realize is that several other events are also fired. |
|
|
|
|
|